<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>CSS布局 | 666-blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/blog-666group/favicon.ico">
    <meta name="description" content="第二届字节跳动青训营-寒假前端场-进阶班-666组">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog-666group/assets/css/0.styles.10636f7a.css" as="style"><link rel="preload" href="/blog-666group/assets/js/app.122a3374.js" as="script"><link rel="preload" href="/blog-666group/assets/js/3.28dd90ed.js" as="script"><link rel="preload" href="/blog-666group/assets/js/1.9c65f412.js" as="script"><link rel="preload" href="/blog-666group/assets/js/15.17f1e81a.js" as="script"><link rel="prefetch" href="/blog-666group/assets/js/10.1f1d3f06.js"><link rel="prefetch" href="/blog-666group/assets/js/11.abeb5294.js"><link rel="prefetch" href="/blog-666group/assets/js/12.6a229917.js"><link rel="prefetch" href="/blog-666group/assets/js/13.da03caea.js"><link rel="prefetch" href="/blog-666group/assets/js/14.ed05c878.js"><link rel="prefetch" href="/blog-666group/assets/js/16.c284d4d7.js"><link rel="prefetch" href="/blog-666group/assets/js/17.485a09bd.js"><link rel="prefetch" href="/blog-666group/assets/js/18.d81ed566.js"><link rel="prefetch" href="/blog-666group/assets/js/19.b6c3f6a6.js"><link rel="prefetch" href="/blog-666group/assets/js/20.92651213.js"><link rel="prefetch" href="/blog-666group/assets/js/21.b73dc2e5.js"><link rel="prefetch" href="/blog-666group/assets/js/22.150f1eb2.js"><link rel="prefetch" href="/blog-666group/assets/js/23.e5a4eb04.js"><link rel="prefetch" href="/blog-666group/assets/js/24.4ab99d4a.js"><link rel="prefetch" href="/blog-666group/assets/js/25.299e8446.js"><link rel="prefetch" href="/blog-666group/assets/js/4.56045e58.js"><link rel="prefetch" href="/blog-666group/assets/js/5.4a4e5b47.js"><link rel="prefetch" href="/blog-666group/assets/js/6.0e562894.js"><link rel="prefetch" href="/blog-666group/assets/js/7.7d3e63ae.js"><link rel="prefetch" href="/blog-666group/assets/js/8.985b535a.js"><link rel="prefetch" href="/blog-666group/assets/js/9.0580e608.js">
    <link rel="stylesheet" href="/blog-666group/assets/css/0.styles.10636f7a.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>666-blog</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>第二届字节跳动青训营-寒假前端场-进阶班-666组</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>666</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog-666group/" class="home-link router-link-active"><img src="/blog-666group/logo.png" alt="666-blog" class="logo"> <span class="site-name">666-blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog-666group/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog-666group/categories/范志旋/" class="nav-link"><i class="undefined"></i>
  范志旋
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/刘宇恒/" class="nav-link"><i class="undefined"></i>
  刘宇恒
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/苏越飞/" class="nav-link"><i class="undefined"></i>
  苏越飞
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/汪宇航/" class="nav-link"><i class="undefined"></i>
  汪宇航
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/薛明坤/" class="nav-link"><i class="undefined"></i>
  薛明坤
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/曾立志/" class="nav-link"><i class="undefined"></i>
  曾立志
</a></li></ul></div></div><div class="nav-item"><a href="/blog-666group/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog-666group/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog-666group/docs/message/messageBoard.html" class="nav-link"><i class="iconfont reco-suggestion"></i>
  留言板
</a></div><div class="nav-item"><a href="/blog-666group/docs/about/author.html" class="nav-link"><i class="iconfont reco-message"></i>
  关于
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/blog-666group/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    666
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>13</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>4</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/blog-666group/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog-666group/categories/范志旋/" class="nav-link"><i class="undefined"></i>
  范志旋
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/刘宇恒/" class="nav-link"><i class="undefined"></i>
  刘宇恒
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/苏越飞/" class="nav-link"><i class="undefined"></i>
  苏越飞
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/汪宇航/" class="nav-link"><i class="undefined"></i>
  汪宇航
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/薛明坤/" class="nav-link"><i class="undefined"></i>
  薛明坤
</a></li><li class="dropdown-item"><!----> <a href="/blog-666group/categories/曾立志/" class="nav-link"><i class="undefined"></i>
  曾立志
</a></li></ul></div></div><div class="nav-item"><a href="/blog-666group/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog-666group/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog-666group/docs/message/messageBoard.html" class="nav-link"><i class="iconfont reco-suggestion"></i>
  留言板
</a></div><div class="nav-item"><a href="/blog-666group/docs/about/author.html" class="nav-link"><i class="iconfont reco-message"></i>
  关于
</a></div> <!----></nav> <ul class="sidebar-links"><li><a href="/blog-666group/blogs/suyuefei/01.html" class="sidebar-link">Promise的静态方法</a></li><li><a href="/blog-666group/blogs/suyuefei/02.html" aria-current="page" class="active sidebar-link">CSS布局</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>CSS布局</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>666</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">CSS布局</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>666</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022/2/7</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>css</span></i></div></div> <div class="theme-reco-content content__default"><hr> <h2 id="前言"><a href="#前言" class="header-anchor">#</a> 前言</h2> <h3 id="常见的布局属性"><a href="#常见的布局属性" class="header-anchor">#</a> 常见的布局属性</h3> <p>（1）<code>display</code> 确定元素的显示类型：</p> <ul><li><p>block：块级元素。</p></li> <li><p>inline：行内元素。</p></li> <li><p>inline-block：对外的表现是行内元素（不会独占一行），对内的表现是块级元素（可以设置宽高）。</p></li></ul> <p>（2）<code>position</code> 确定元素的位置：</p> <ul><li><p>static：默认属性值。</p></li> <li><p>relative：相对定位。相对于元素本身进行偏移，<strong>不会改变它所占据的空间</strong>。</p></li> <li><p>absolute：绝对定位。相对于父元素中最近的 relative/absolute 进行偏移，会脱离文档流。音标：[ˈæbsəluːt]。</p></li> <li><p>fixed：固定定位。相对于可视区域固定，会脱离文档流。</p></li></ul> <p><code>relative</code>、<code>absolute</code>、<code>fixed</code>这三个属性，可以结合 z-index 来设置层级。</p> <h3 id="常见的布局方法"><a href="#常见的布局方法" class="header-anchor">#</a> 常见的布局方法</h3> <p>1、<strong>table 表格布局</strong>：早期使用的布局，如今用得很少。</p> <p>2、<strong>float 浮动 + margin</strong>：为了兼容低版本的IE浏览器，很多网站（比如腾讯新闻、网易新闻、淘宝等）都会采用 float 布局。</p> <p>3、<strong>inline-block 布局</strong>：对外的表现是行内元素（不会独占一行），对内的表现是块级元素（可以设置宽高）。</p> <p>4、<strong>flex 布局</strong>：为布局而生，非常灵活，是最为推荐的布局写法。</p> <p>唯一的缺点是兼容性问题：</p> <p><img src="http://img.smyhvae.com/20191005_1200.png" alt=""></p> <p>上图中可以看到， flex 布局不支持 IE9 及以下的版本。如果你的页面不需要处理 IE浏览器的兼容性问题，则可以放心大胆地使用 flex 布局。</p> <p>flex 是一种现代的布局方式，是 W3C 第一次提供真正用于布局的 CSS 规范。</p> <p>5、响应式布局。</p> <h2 id="float-布局"><a href="#float-布局" class="header-anchor">#</a> float 布局</h2> <p>是 CSS 中一种比较麻烦的属性，涉及到 BFC 和清除浮动（面试的重点）。</p> <h3 id="float-属性的特点"><a href="#float-属性的特点" class="header-anchor">#</a> float 属性的特点</h3> <ul><li><p>元素浮动</p></li> <li><p><strong>脱离文档流，但不脱离文本流</strong></p></li></ul> <p>代码举例：</p> <p>下面这两个并列的<code>div1</code>和<code>div2</code>，默认是在标准流中的：</p> <p><img src="http://img.smyhvae.com/20191005_2029.png" alt=""></p> <p>在此基础之上，如果给<code>div1</code>增加<code>float: left</code>属性后，效果如下：</p> <p><img src="http://img.smyhvae.com/20191005_2037.png" alt=""></p> <p>上图中，可以看到，<code>div1</code>设置为浮动后，会脱离文档流，不会对<code>div2</code>的布局造成影响；但是<code>div1</code>不会脱离文本流，它会影响<code>div2</code>中文字的排列。</p> <p>其实，这正是 float 属性的作用。float 本身是用来做图文混排、文字环绕的效果。</p> <h3 id="float-所带来的影响"><a href="#float-所带来的影响" class="header-anchor">#</a> float 所带来的影响</h3> <p><strong>1、对自身的影响</strong>：</p> <ul><li><p>形成“块”（BFC）</p></li> <li><p>位置尽量靠上</p></li> <li><p>位置尽量靠左/右</p></li></ul> <p>下面这两个并列的<code>div1</code>和<code>div2</code>，设置为浮动之后的效果：（都是尽量靠左显示的）</p> <p><img src="http://img.smyhvae.com/20191005_2130.png" alt=""></p> <p>在上方代码的基础之上，增加 <code>div2</code>的宽度之后，会发现，<code>div2</code>掉下来了：</p> <p><img src="http://img.smyhvae.com/20191005_2135.png" alt=""></p> <p><strong>2、对兄弟元素的影响</strong>：</p> <ul><li><p>不影响其他块级元素的位置</p></li> <li><p>影响其他块级元素的内部文本</p></li></ul> <p><strong>3、对父级元素的影响</strong>：</p> <ul><li><p>从父级的布局中“消失”</p></li> <li><p>造成父级元素的高度塌陷：父级元素撑开 div1 之后（父级元素里没有其他元素的情况下），如果设置 div1 为 float 之后，，会让父级元素的高度变为0。</p></li></ul> <h2 id="inline-block-布局"><a href="#inline-block-布局" class="header-anchor">#</a> inline-block 布局</h2> <p>对外的表现是行内元素（不会独占一行），对内的表现是块级元素（可以设置宽高）。</p> <p><strong>思路</strong>：像文本一样去排列 block 元素，没有清除浮动等问题。</p> <p><strong>存在的问题</strong>：需要处理间隙。代码举例如下：</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>viewport<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>width=device-width, initial-scale=1.0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>Document<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
	<span class="token selector">.container</span><span class="token punctuation">{</span>
		<span class="token property">width</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span>
		<span class="token property">height</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span>
		<span class="token property">background</span><span class="token punctuation">:</span> pink<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">.div1</span><span class="token punctuation">{</span>
		<span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
		<span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
		<span class="token property">background</span><span class="token punctuation">:</span> green<span class="token punctuation">;</span>
		<span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">.div2</span><span class="token punctuation">{</span>
		<span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
		<span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
		<span class="token property">background</span><span class="token punctuation">:</span> yellowgreen<span class="token punctuation">;</span>
		<span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token selector">.div3</span><span class="token punctuation">{</span>
		<span class="token property">background</span><span class="token punctuation">:</span> yellow<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>div1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>div1的inline-block 属性<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>div2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>div2的inline-block 属性<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>div3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
			琴棋书画不会，洗衣做饭嫌累。
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><p><img src="http://img.smyhvae.com/20191005_2200.png" alt=""></p> <p>上面的代码，存在两个问题。</p> <p><strong>问题一</strong>：如果设置<code>div2</code>的宽度为 200px 之后，<code>div2</code> 掉下来。</p> <p><strong>问题二</strong>：<code>div1</code>和<code>div2</code>设置为 inline-block之后，这两个盒子之间存在了间隙。这是因为，此时的 <code>div1</code>和<code>div2</code> 已经被当成文本了。文本和文本之间，本身就会存在间隙。</p> <p>为了去掉这个间隙，可以有几种解决办法：</p> <p>办法1：设置父元素<code>container</code>的字体大小为0，即<code>font-size: 0</code>，然后设置子元素 <code>div1</code>、<code>div2</code>的字体<code>font-size: 12px</code>。</p> <p>办法2：在写法上，去掉<code>div1</code>和<code>div2</code>之间的换行。改为：</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>div1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>div1的inline-block 属性<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>div2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>div2的inline-block 属性<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="响应式布局"><a href="#响应式布局" class="header-anchor">#</a> 响应式布局</h2> <p>移动端用得较多，待学习补充。</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/2/8 下午8:56:42</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blog-666group/blogs/suyuefei/01.html" class="prev">
            Promise的静态方法
          </a></span> <!----></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#前言" class="sidebar-link reco-side-前言" data-v-cb1513f6>前言</a></li><li class="level-3" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#常见的布局属性" class="sidebar-link reco-side-常见的布局属性" data-v-cb1513f6>常见的布局属性</a></li><li class="level-3" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#常见的布局方法" class="sidebar-link reco-side-常见的布局方法" data-v-cb1513f6>常见的布局方法</a></li><li class="level-2" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#float-布局" class="sidebar-link reco-side-float-布局" data-v-cb1513f6>float 布局</a></li><li class="level-3" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#float-属性的特点" class="sidebar-link reco-side-float-属性的特点" data-v-cb1513f6>float 属性的特点</a></li><li class="level-3" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#float-所带来的影响" class="sidebar-link reco-side-float-所带来的影响" data-v-cb1513f6>float 所带来的影响</a></li><li class="level-2" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#inline-block-布局" class="sidebar-link reco-side-inline-block-布局" data-v-cb1513f6>inline-block 布局</a></li><li class="level-2" data-v-cb1513f6><a href="/blog-666group/blogs/suyuefei/02.html#响应式布局" class="sidebar-link reco-side-响应式布局" data-v-cb1513f6>响应式布局</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="reco-bgm-panel" data-v-b1d3339e><audio id="bgm" src="./music/01.mp3" data-v-b1d3339e></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><img src="./music/千与千寻.png" data-v-b1d3339e></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="reco-bgm-cover" style="background-image:url(./music/千与千寻.png);" data-v-b1d3339e><div class="mini-operation" style="display:none;" data-v-b1d3339e><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-b1d3339e></i></div> <div class="falut-message" style="display:none;" data-v-b1d3339e>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-music music" data-v-b1d3339e></i>千与千寻</div> <div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-artist" data-v-b1d3339e></i>龙舟</div> <div class="reco-bgm-progress" data-v-b1d3339e><div class="progress-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div> <div class="reco-bgm-operation" data-v-b1d3339e><i class="reco-bgm reco-bgm-last last" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play play" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-next next" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-b1d3339e></i> <div class="volume-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div></div> <div class="reco-bgm-left-box" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><i class="reco-bgm reco-bgm-left" data-v-b1d3339e></i></div></div></div><div></div><div></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:290px;display:none;" data-v-5775ee02>
      欢迎来到我的博客
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="250" height="320" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><!----><div class="RibbonAnimation"></div></div></div>
    <script src="/blog-666group/assets/js/app.122a3374.js" defer></script><script src="/blog-666group/assets/js/3.28dd90ed.js" defer></script><script src="/blog-666group/assets/js/1.9c65f412.js" defer></script><script src="/blog-666group/assets/js/15.17f1e81a.js" defer></script>
  </body>
</html>
